#include"../tcp_server.hpp"

class EchoServer
{
private:
    void OnConnected(const PtrConnection& conn){
        DLOG("NEW CONNECTION:%p",conn.get());
    }
    void OnClosed(const PtrConnection& conn){
        DLOG("CLOSE CONNECTION:%p",conn.get());
    }
    void MessageHandler(const PtrConnection&conn,Buffer*buffer){
        DLOG("%s",buffer->ReadPosition());
        std::string str=buffer->ReadPosition();
        buffer->MoveReadOffset(buffer->ReadAlbeSize());
        conn->Send(str.c_str(),str.size());
        conn->Shutdown();
    }
public:
    EchoServer(int port):_tcp_server(port){
        _tcp_server.SetThreadCount(3);
        _tcp_server.EnableInactiveRelease(10);
        _tcp_server.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _tcp_server.SetMessageCallback(std::bind(&EchoServer::MessageHandler,this,std::placeholders::_1,std::placeholders::_2));
        _tcp_server.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
    }
    void Start(){_tcp_server.Start();}
private:
    TcpServer _tcp_server;
};